#include "SWKNXListXml.h"
#include "Main/GateWay.h"
SWKNXListXml::SWKNXListXml()
{
    m_knxDataList.clear();
     m_sceneMap.clear();
}

QMap<SWKNXDataStruct, QList<ControlPointStruct> > SWKNXListXml::sceneMap() const
{
    return m_sceneMap;
}

QList<ControlPointStruct> SWKNXListXml::controlPointList(const SWKNXDataStruct &controlPoint)
{
    QMap<SWKNXDataStruct,QList<ControlPointStruct> >::iterator it;
    for(it=m_sceneMap.begin();it!=m_sceneMap.end();++it)
    {
        if(controlPoint == it.key())
        {


            if(it.value().size() > 0)
            {
                qDebug()<< it.value().at(0).address()<<","<<it.value().at(0).value();
                return it.value();
            }

        }
    }

   return m_sceneMap.value(controlPoint);
}

QList<ControlPointStruct> SWKNXListXml::controlPointList(const QList<SWKNXDataStruct> &sourceKNXList)
{
    QList<ControlPointStruct> controlPointList;
    foreach (const SWKNXDataStruct &sourceKNX, sourceKNXList)
    {
        controlPointList.append(this->controlPointList(sourceKNX));
    }


    return controlPointList;
}


QList<SWKNXDataStruct> SWKNXListXml::knxDataList() const
{
    return m_knxDataList;
}

bool SWKNXListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "KNXPT")
        {


            SWKNXDataStruct knxPoint;
             QStringList strTemp = rootChild.toElement().attribute("GroupAddress").split("/");
              QByteArray dataTemp;
             QStringList::const_iterator it;
             for(it=strTemp.constBegin();it != strTemp.constEnd();++it)
                 dataTemp.append((*it).toInt());
             if(dataTemp.size()==3)
             {
                 quint16 address;
                 address = dataTemp.at(0)*2048+dataTemp.at(1)*256+dataTemp.at(2);
                 knxPoint.setGroupAddress(address);
                  qDebug()<<"XML groupaddress is "<<address;
             }
             else
            knxPoint.setGroupAddress(0);

             dataTemp.clear();
             strTemp = rootChild.toElement().attribute("FB").split("/");
             for(it=strTemp.constBegin();it != strTemp.constEnd();++it)
                 dataTemp.append((*it).toInt());
             if(dataTemp.size()==3)
             {
                 quint16 address;
                 address = dataTemp.at(0)*2048+dataTemp.at(1)*256+dataTemp.at(2);
                 knxPoint.setFBAddress(address);
                  qDebug()<<"XML FB groupaddress is "<<address;
             }
             else
            knxPoint.setFBAddress(0);


            knxPoint.setKnxLen(rootChild.toElement().attribute("Len").toInt());
             strTemp = rootChild.toElement().attribute("Value").split(",");

           // QStringList::const_iterator it;
             dataTemp.clear();
            for(it=strTemp.constBegin();it != strTemp.constEnd();++it)
                dataTemp.append((*it).toInt());
            knxPoint.setDataList(dataTemp);

          //  qDebug()<<"SWKNXListXml-"<<DataTypeConver::formatString(dataTemp);

            QList<ControlPointStruct> controlPointList;
            QDomNode resolvePTChild = rootChild.toElement().firstChild();
            while(!resolvePTChild.isNull())
            {
                if (resolvePTChild.toElement().tagName() == "ControlPT")  //解析控制点位
                {
                    ControlPointStruct controlPoint;
                    controlPoint.clearnPortList();
                    strTemp = resolvePTChild.toElement().text().split(",");
                    controlPoint.setAddress(strTemp.at(0).toInt());
                    controlPoint.setValue(strTemp.at(1).toInt());
                    controlPointList.append(controlPoint);

                }
                else if (resolvePTChild.toElement().tagName() == "ThirdPT") // 解析 第三方完整命令
                {
                    ControlPointStruct controlPoint;
                    controlPoint.clearnPortList();
                    controlPoint.setAddress(0);
                    controlPoint.setValue(0);
                    strTemp = resolvePTChild.toElement().text().split(",");
                    quint8 len = strTemp.at(0).toInt();
                    if((len > 0)&&(strTemp.size()>=len+1))
                    {
                        controlPoint.setThirdFuncLen(len);
                        QByteArray list;
                        for(int i=0;i<len;i++)
                        {
                        list.append(strTemp.at(i+1).toInt(0,16));
                        }
                        controlPoint.setThirdFuncList(list);

                        if(strTemp.size() > len+1)
                        {
                             controlPoint.setDelayTime(strTemp.at(len+1).toInt());
                        }
                        controlPointList.append(controlPoint);
                    }

           }
                resolvePTChild = resolvePTChild.nextSibling();
            }

             m_sceneMap.insert(knxPoint,controlPointList);
             {
                 qDebug()<<"************************";
                 qDebug()<<"knx gropuAddress "<<knxPoint.groupAddress();
                 qDebug()<<"knx len "<<knxPoint.knxLen();
                 qDebug()<<"knx dataList "<<DataTypeConver::formatString(knxPoint.dataList());
                 qDebug()<<"point address "<<controlPointList.at(0).address();
                 qDebug()<<"point value "<<controlPointList.at(0).value();


                 qDebug()<<"************************";
             }
            knxPoint.setControlPointList(controlPointList);
             m_knxDataList.append(knxPoint);


    }
        rootChild = rootChild.nextSibling();
    }
    qDebug()<<"m_knxDataList size "<<m_knxDataList.size();
    return true;
}

bool SWKNXListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_knxDataList.clear();
    return true;
}
/*
QList<ControlPointStruct> SWKNXListXml::controlPointList(const SWKNXDataStruct &controlPoint)
{
    quint16 groupAddress = controlPoint.groupAddress();
    quint16 len   = controlPoint.knxLen();
    QList<ControlPointStruct> controlPointList;
    for(int i=0; i<m_knxDataList.size(); i++)
    {
        SWKNXDataStruct rangePoint = m_knxDataList.at(i);
        if((groupAddress == rangePoint.groupAddress())&&(len==rangePoint.knxLen()))
        {
            if(controlPoint.dataList()==rangePoint.dataList())
            {
                controlPointList =  rangePoint.controlPointList();
            }
        }
    }
    return controlPointList;
}

QList<ControlPointStruct> SWKNXListXml::controlPointList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<ControlPointStruct> controlPointList;
    foreach (const ControlPointStruct &sourcePoint, sourcePointList)
    {
        controlPointList.append(this->controlPointList(sourcePoint));
    }
    return controlPointList;
}*/

